package com.fuerda.izb.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.fuerda.izb.R

class ShadowLineView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var hasShadow = true // 控制是否显示阴影的布尔变量
    private var cornerRadius=0f

    private val rectPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        color = Color.BLUE
    }

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShadowLineView)
        try {
            rectPaint.strokeWidth = typedArray.getDimension(R.styleable.ShadowLineView_stroke_width, 2f)
            cornerRadius = typedArray.getDimension(R.styleable.ShadowLineView_corner_radius, 8f)
            rectPaint.color = typedArray.getColor(R.styleable.ShadowLineView_shadow_color, Color.BLUE)

            // 如果想要在XML中直接指定是否开启阴影，也可以增加相应的属性
            // hasShadow = typedArray.getBoolean(R.styleable.CustomRoundedRectView_has_shadow, true)

            // 但通常阴影的其他属性（如模糊半径、偏移量等）不会在XML中指定，因为它们更倾向于动态改变
        } finally {
            typedArray.recycle()
        }
    }

    fun setShadowEnabled(enabled: Boolean) {
        hasShadow = enabled
        if (enabled) {
            rectPaint.setShadowLayer(
                cornerRadius, // 这里可以根据需求定义默认值，也可以作为自定义属性
              2f,
              2f,
            rectPaint.color // 使用线条颜色作为阴影颜色，也可以在此处修改为单独定义的阴影颜色
            )
        } else {
            rectPaint.setShadowLayer(0f, 0f, 0f, Color.TRANSPARENT)
        }
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val rect = RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
        canvas.drawRoundRect(rect, rectPaint.strokeWidth / 2, rectPaint.strokeWidth / 2, rectPaint)
    }


}